package main.Q801_900;

import java.util.Stack;

public class Q851_860 {
    public static void main(String[] args) {
        System.out.println("Question851：");
        System.out.println("Question852：山脉数组的峰顶索引");
        System.out.println("Question853：");
        System.out.println("Question854：");
        System.out.println("Question855：");
        System.out.println("Question856：括号的分数");
        System.out.println("Question857：");
        System.out.println("Question858：");
        System.out.println("Question859：");
        System.out.println("Question860：柠檬水找零");
    }
}

class Question852{
    public int peakIndexInMountainArray(int[] arr) {
        int left=0,right=arr.length,index=0;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (arr[mid]>arr[mid-1]&&arr[mid]>arr[mid+1]){
                index=mid;
                break;
            }else if (arr[mid-1]<arr[mid]&&arr[mid]<arr[mid+1]) left=mid;
            else right=mid;
        }
        return index;
    }
}

class Question856{
    public static int scoreOfParentheses(String s) {
        Stack<String> stack=new Stack<>();
        char[] str=s.toCharArray();
        for (int i=0;i<str.length;i++){
            if (stack.isEmpty()||(stack.peek().equals("(")&&str[i]=='(')) stack.push("(");//栈顶和入栈元素均为"("
            else if (stack.peek().equals("(")&&str[i]==')') {//栈顶元素与入栈元素组成一对括号
                stack.pop();
                stack.push("1");//成对括号入1
            }else if (!stack.peek().equals("(")&&str[i]=='(') stack.push("(");//栈顶元素为数字且入栈元素为"("，不能运算
            else if (!stack.peek().equals("(")&&str[i]==')'){//栈顶元素为数字且入栈元素为")"，能够进行运算
                int sum=0;
                while (!stack.peek().equals("(")){//累加本组括号中的数字
                    sum+=Integer.valueOf(stack.pop());
                }
                stack.pop();//将栈顶的"("弹出
                stack.push(String.valueOf(sum*2));
            }
        }
        int result=0;
        while (!stack.isEmpty()){
            result+=Integer.valueOf(stack.pop());
        }
        return result;
    }
}

class Question860{
    public boolean lemonadeChange(int[] bills) {
        int count5=0,count10=0,count20=0;
        for (int bill:bills){
            if (bill==5) count5++;
            else if (bill==10) {
                if (count5==0) return false;
                else {
                    count10++;
                    count5--;
                }
            }else if (bill==20){
                if (count10!=0&&count5!=0){//10找
                    count10--;
                    count5--;
                    count20++;
                }else if (count5>=3&&count10==0){//5找
                    count5-=3;
                    count20++;
                }else return false;//不能找
            }
        }
        return true;
    }
}